import request from '@/util/request';
import type { Note, PageResponse, ApiResponse } from '@/types/note/note';
// import { SignUtil } from '@/util/signin';

// 获取笔记列表（分页）
export const getNotes = async (): Promise<ApiResponse<PageResponse<Note>>> => {
    // const userId = SignUtil.getUserId();
    // if (!userId) {
    //     throw new Error('请先登录');
    // }

    try {
        const response = await request.get<ApiResponse<PageResponse<Note>>>('api/note/list', {
            params: null
        });
        return response.data;
    } catch (error) {
        console.error('Failed to fetch notes:', error);
        throw error;
    }
};

// 创建笔记 - 移除 noteId，由后端自增生成
export const createNote = (note: Omit<Note, 'noteId' | 'createTime' | 'updateTime' | 'createBy' | 'updateBy'>) => {
    return request.post<ApiResponse<Note>>('api/note/create', note);
};

// 更新笔记 - 只更新允许的字段
export const updateNote = (id: number, note: Pick<Note, 'noteTitle' | 'noteContent' | 'isPublic'>) => {
    return request.put<ApiResponse<Note>>(`api/note/update/${id}`, note);
};

// 删除笔记
export const deleteNote = (id: number) => {
    return request.delete<ApiResponse<Note>>(`api/note/delete/${id}`);
};

// 批量删除笔记
export const batchDeleteNotes = (ids: number[]) => {
    return request.post<ApiResponse<Note[]>>('api/note/batch-delete', { ids });
};

// 搜索笔记
export const searchNotes = (query: string) => {
    return request.get<ApiResponse<Note[]>>(`api/note/search?query=${encodeURIComponent(query)}`);
}; 